package cn.controller;

import cn.service.impl.CategoryAttributesServiceImpl;
import cn.service.impl.CategoryServiceImpl;
import entity.Category;
import entity.Result;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;

@RestController
@CrossOrigin
public class CategoryController {
    @Resource
    CategoryServiceImpl categoryService;

    @RequestMapping("/queryCategories")
    public Result queryCategories(Integer currentPage,Integer pageSize) {
        return categoryService.queryCategories(currentPage,pageSize);
    }

    @RequestMapping("/updateNav_status")
    public void updateNav_status(Long category_id,Integer nav_status) {
        categoryService.updateNav_status(category_id,nav_status);
    }

    @RequestMapping("/updateShow_status")
    public void updateShow_status(Long category_id,Integer show_status) {
        categoryService.updateShow_status(category_id,show_status);
    }

    @RequestMapping("/deleteCategoryRowLevel0")
    public Result deleteCategoryRowLevel0(Long category_id) {
        return categoryService.deleteByPrimaryKey(category_id);
    }

    @RequestMapping("/iconUpload")
    public Result iconUpload(MultipartFile multipartFile) {
        //将图片存入后端,并返回一个图片地址
        try {
            String path = ResourceUtils.getURL("classpath:").getPath() + "static/images/";
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            InputStream inputStream = multipartFile.getInputStream();
            String fileName = multipartFile.getOriginalFilename();
            fileName = System.currentTimeMillis() + fileName;
            OutputStream outputStream = new FileOutputStream(path + fileName);
            byte[] b = new byte[1024 * 1024];
            int d = -1;
            while ((d = inputStream.read(b)) != -1) {
                outputStream.write(b,0,d);
                outputStream.flush();
            }
            outputStream.close();
            String fileUrl = "http://localhost:8080/images/" + fileName;
            return Result.success(fileUrl);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("/addCategoryLevel0")
    public Result addCategoryLevel0(@RequestBody Category category) {
        return categoryService.addCategoryLevel0(category);
    }

    @RequestMapping("/queryByCategory_Id")
    public Result queryByCategory_Id(Long category_id) {
        return categoryService.queryByCategory_Id(category_id);
    }

    @RequestMapping("/categoryEditLevel0")
    public Result categoryEditLevel0(@RequestBody Category category) {
        return categoryService.categoryEditLevel0(category);
    }

    @RequestMapping("/queryCategoriesLevel1")
    public Result queryCategoriesLevel1(Long category_id,Integer currentPage,Integer pageSize) {
        return categoryService.queryCategoriesLevel1(category_id,currentPage,pageSize);
    }

}
